
%put stat(dsetin,dsetout,vars,stats,bygroup=none);

%macro stat(dsetin,dsetout,vars,stats,bygroup=none);
/*first CS stat,then TS average*/
data dsettemp;
set &dsetin;
run;

%if &bygroup=none %then %do;
data dsettemp;
set dsettemp;
tempgroup=1;
run;
%let bygroup=tempgroup;
%end;

proc sort data=dsettemp;
by &bygroup;
run;
%let vars=%trim(&vars);
%let stats=%trim(&stats);
%let nvar=%EVAL(%SYSFUNC(countw(&vars)));
%let nstat=%EVAL(%SYSFUNC(countw(&stats)));

%local i;
%let stat_opt=;  /* statitstics*/
%do i=1 %to &nstat;
%let stat_opt=&stat_opt %QSCAN(&stats,&i,%STR(  ))=%QSCAN(&stats,&i,%STR(  )); 
%end;
%let stat_opt=%trim(&stat_opt);

%local j;
%do j=1 %to &nvar;
%let var=%scan(&vars, &j);
proc means data=dsettemp noprint;
by &bygroup;
var &var;
output out=temp(drop=_TYPE_ _FREQ_) &stat_opt; /*could be modified*/
run;

data temp;
set temp;
format factor $20.;
factor="&var";
label &stat_opt;
run;

%if %sysevalf(&j=1) %then %do;
data outtemp;
set temp;
%end;
%else %do;
data outtemp;
set outtemp temp;
run;
%end;

%end;


proc sort data=outtemp;
by factor;
run;


proc means data=outtemp  noprint ;
var &stats;
by factor;
output out=&dsetout(drop=_type_ _freq_) mean=&stats;
run;

proc datasets library=work nolist;
delete dsettemp temp outtemp;
quit;

%mend;




*%stat(pred_use,table1,BP EP CP ACC NOA RD ADV GPOA CPEX SG AG dE dD ,p25 mean median p75 std skew kurt,bygroup=year_Jun);
